<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 21/01/2019
 * Time: 14:28
 */

namespace App\Services\UCenter\UCenter;


use App\Models\Car\Car;

class CarLogic
{
    /**
     * 添加车辆
     * @param $data
     * @return array
     */
    public function addCars($data)
    {
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');
        try {
            //检测车牌号是否存在
            $checkRes = Car::where('number', $data['number'])->first();
            if($checkRes)
                return auto_failure(null, '该车牌号已存在');
            $res = Car::insert($data);
            return $res ?
                auto_success($res, '添加成功') :
                auto_failure($res, '添加失败');
        } catch (\Exception $e) {
            auto_failure($e->getMessage(),'添加失败');
        }
    }

    public function changeCarInfo($data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s');
        $carMod = Car::where(['user_id' => $data['user_id'], 'id' => $data['id']])->first();
        foreach ($data as $k => $v) {
            $carMod->$k = $v;
        }
        $res = $carMod->save();
        return $res ?
            auto_success($res, '修改成功') :
            auto_failure($res, '修改失败');
    }

    /**
     * 获取车辆列表
     * @param $data
     * @return array
     */
    public function getCarList($data)
    {
        $_where = [
            'user_id' => $data['user_id'],
            'is_del' => 0
        ];
        $query = Car::withCount(['carDriver'=>function($query){$query->where('status',1);}])->where($_where);
        !empty($data['number']) && $query->where('number',$data['number']);
        !empty($data['car_length']) && $query->where('car_length',$data['car_length']);
        !empty($data['weight']) && $query->where('weight',$data['weight']);
        !empty($data['plate_type']) && $query->where('plate_type',$data['plate_type']);
        !empty($data['is_busy']) && $query->where('is_busy',0);
        $res = $query->paginate(8);
        return $res ?
            auto_success($res, '获取成功') :
            auto_failure($res, '获取失败');
    }

    /**
     * 根据id删除车辆信息
     * @param $data
     * @return array
     */
    public function delCarById($data)
    {
        $carMod = Car::where(['user_id' => $data['user_id'], 'id' => $data['id']])->first();
        $carMod->is_del = 1;
        $res = $carMod->save();
        return $res ?
            auto_success($res, '删除成功') :
            auto_failure($res, '删除失败');
    }

    /**
     * 根据id获取车辆详情
     * @param $data
     * @return array
     */
    public function getCarById($data)
    {
        $res = Car::where(['id' => $data['id'], 'user_id' => $data['user_id']])->first();
        return $res ?
            auto_success($res, '获取成功') :
            auto_failure($res, '获取失败');
    }

    /**
     * 获取车辆列表
     */
    public function backCarList($data)
    {
        $query = Car::where('is_del', 0);
        if($data['keywords']){
            $keyword = $data['keywords'];
            $query->where(function ($query) use ($keyword) {
                $query->where('user_id','like',"%".$keyword."%")->orWhere('owner','like',"%".$keyword."%")->orWhere('number','like',"%".$keyword."%");
            });
        }
        $res = $query->paginate(10);
        return $res ? auto_success($res, '获取成功') : auto_failure($res, '获取失败');
    }

    /**
     * 管理员获取车辆详情
     */
    public function backCarInfo($data)
    {
        $res = Car::where('id', $data['id'])->first();
        return auto_return($res, '获取成功', '获取失败');
    }

    /**
     * 管理员审核车辆
     */
    public function backCarAudit($data)
    {
        $res = Car::where('id', $data['id'])->update(['status' => $data['status']]);
        return auto_return($res, '审核成功', '审核失败');
    }
}
